package com.kwshare.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kwshare.common.core.domain.user.User;
import com.kwshare.user.mapper.UserMapper;
import com.kwshare.user.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用户接口
 *
 * @author yangjing
 */
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 根据用户id查询
     *
     * @param username 文件
     * @return 结果
     */
    @Override
    public User getUserByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("del_flag",0);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 新增用户
     *
     * @param user 用户
     * @param createBy 创建者
     * @return 结果
     */
    @Override
    public int insertUser(User user, String createBy) {
        user.setCreateBy(createBy);
        user.setCreateTime(new Date());
        return userMapper.insert(user);
    }

    /**
     * 查询用户列表信息
     *
     * @param username 账号
     * @param name 姓名
     * @return
     */
    @Override
    public List<User> getUserList(String username,String name) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username",username);
        queryWrapper.like("name",name);
        queryWrapper.eq("del_flag",0);
        return userMapper.selectList(queryWrapper);
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return 结果
     */
    @Override
    public int deleteUserById(Long id) {
        UpdateWrapper<User> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("del_flag",0);
        queryWrapper.set("del_flag",2);
        return userMapper.update(null,queryWrapper);
    }

    /**
     * 修改用户
     * @param user 用户
     * @param updateBy 创建者
     * @return 结果
     */
    @Override
    public int updateUser(User user, String updateBy) {
        UpdateWrapper<User> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("id",user.getId());
        queryWrapper.eq("del_flag",0);
        user.setUpdateBy(updateBy);
        user.setUpdateTime(new Date());
        return userMapper.update(user,queryWrapper);
    }
}
